package com.btex.app.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lidroid.xutils.BitmapUtils;
import com.btex.app.R;
import com.btex.app.entity.MyCoinsInfo;
import com.btex.app.tools.LogUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wcx on 16/3/20.
 */
public class MyCoinsAdapter extends BaseAdapter{
    private Context cxt;
    private List<MyCoinsInfo> data = new ArrayList<>();
    private String TAG = getClass().getSimpleName();

    public MyCoinsAdapter(Context cxt) {
        this.cxt = cxt;
    }

    @Override
    public int getCount() {
        return data==null?0:data.size();
    }

    public void addData(boolean clear, List<MyCoinsInfo> myCoinsInfos){
        if(clear){
            data.clear();
            LogUtil.i(TAG, "刷新数据");
            LogUtil.i(TAG,"coinTradeHistoryInfos:"+myCoinsInfos.toString());
        }
        data.addAll(myCoinsInfos);
        notifyDataSetChanged();
    }

    @Override
    public Object getItem(int position) {
        return (data==null||data.size()==0)?null : (data.size()>=position?data.get(position):null);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(cxt).inflate(
                    R.layout.item_my_account, null);
            holder = new ViewHolder();
            holder.ivCoinHeader = (ImageView) convertView.findViewById(R.id.iv_coin_header);
            holder.tvCoinName = (TextView) convertView.findViewById(R.id.tv_coin_name);
            holder.tvAvilableCoin = (TextView) convertView.findViewById(R.id.tv_avilableCoin);
            holder.tvFrozenCoin = (TextView) convertView.findViewById(R.id.tv_frozenCoin);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        MyCoinsInfo myCoinsInfo = (MyCoinsInfo) getItem(position);
        if(myCoinsInfo!=null) {
            holder.tvCoinName.setText(myCoinsInfo.getType());
            holder.tvAvilableCoin.setText(myCoinsInfo.getTotal());
            holder.tvFrozenCoin.setText(myCoinsInfo.getFrozen_guzhi());
            new BitmapUtils(cxt).display(holder.ivCoinHeader,myCoinsInfo.getBalance_guzhi());
        }

        return convertView;
    }


    private static class ViewHolder{
        private ImageView ivCoinHeader;
        private TextView tvCoinName;
        private TextView tvAvilableCoin;
        private TextView tvFrozenCoin;
    }

}
